package com.oceansoft.mobile.econsole.modules.weixin.controller;

import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by xuhb on 2016/4/12.
 */
@Controller
@RequestMapping("weixin/seal")
public class SealQueryController extends BaseController {
    private final String sealUri = "http://139.210.38.71/WebService/PublicService.asmx";
    private final String soapaction="http://tempuri.org/";   //域名，这是在server定义的
    /**
     * 印章查询首页
     */
    @RequestMapping(value = "sealIndex", method = GET)
    public String gotoIndex(HttpServletRequest request, Model model){
        return "weixin/bmsrv/yzcx";
    }

    @RequestMapping(value="querySeal")
    @ResponseBody
    public String sealQuery(HttpServletRequest request, Model model){
        // 印章ID
        String yzid = request.getParameter("yzid");
        // 密码
        String pass = request.getParameter("pass");
        String result = "";
        try{
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(sealUri);
            // 设置operation 名称，
            call.setOperationName(new QName(soapaction, "ExistsYz"));
            // 设置参数
            call.addParameter(new QName(soapaction, "yzid"), XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName(soapaction, "pass"), XMLType.XSD_STRING, ParameterMode.IN);
            // 设置返回值类型
            call.setReturnType(XMLType.XSD_STRING);
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapaction+"ExistsYz");

            Object[] params = new Object[2];
            params[0] = yzid;//"2201021005046";
            params[1] = pass;//"120012";
            result = (String) call.invoke(params);
            //{"Success","true","Message","查询的印章存在！"}
            //{"Success","false","Message","查询错误"}
            if(result.indexOf("查询的印章存在")>0){

                result = "true";
            } else if(result.indexOf("查询的印章不存在")>0){
                result = "false";
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        return result;
    }
}
